<?
    require_once("objects/link_button.php");
    
    class Listing {
        private $_id;
        private $_basic;
        private $_titles;
        private $_data;
        private $_important;
        var $info;
        
        function __construct($structure,$options) {
            $this->_titles = $structure;
            $this->_options = $options;
            
            $this->info =& $this->_basic;           
        }
        function GetLandlord($idonly = false) {
        	$query = "SELECT * FROM guests WHERE id=" . $this->_basic['landlordid'] .";";
        	$landlord = DBM::FetchRow($query);
        	if ($idonly) return $landlord['id'];
        	else return $landlord;
        }
        function SetBasicData($arr) {
            $this->_basic = $arr;
            if ($this->_basic['id'])
                $this->_id = $this->_basic['id'];
        }
        function AddDetail($structureid, $data) {
            $this->_data[$structureid] = (isset($this->_options[$structureid]) ? $this->parseData($structureid,$data) : $data);
        }
        function SetImportantFields($fields) {
            $this->_important = $fields;
        }
        function GetAddress($multiline = false,$returnobj = false) {
            $addr = new AddrString($this->_basic,$multiline);
            if ($returnobj) return $addr;
            else return $addr->Render(true);
        }
        function GetRent() {
            return $this->_basic['rent'];
        }
        private function getDetails($idarr) {
            $ret = array();
            foreach ($idarr as $id) {
                $ret[$this->_titles[$id]] = $this->_data[$id];
            }
            
            return $ret;
        }
        
        function GetID() { return $this->_id; }
        function GetImportantDetails() {
            return $this->getDetails($this->_important);
        }
        function GetAllDetails() {
            return $this->getDetails($this->_data);
        }

        function IsFavorite($userid = NULL) {
            $userid = (is_null($userid) ? AuthManager::GetUserID() : $userid);
            $query = "SELECT * FROM favorites WHERE userid = $userid AND listingid = " . $this->GetID() . ";";
            return (DBM::CountRows($query) > 0);
        }
        
        private function parseData($structid,$data) {
            // take an array of values from the database and convert it into the titles specified in the listing structure
            $data = explode(";",$data);
            $options = explode(",",$this->_options[$structid]);
            $optionsassoc = array();
            foreach ($options as $option) {
                $optionsassoc[SettingManager::EncodeSettingName($option)] = $option;
            }
            
            $ret = array();
            
            foreach ($data as $opt) {
                $parts = explode(":",$opt);
                $ret[] = $optionsassoc[$parts[0]];
            }
            $ret = implode(",",$ret);
            $this->_data[$structid] = $ret;
            return $ret;
        }
        
        function GetImages() {
            return ListingImageManager::GetImages($this->_id);
        }
        function GetThumbnail($index = 0) {
            return ListingImageManager::GetAThumbnail($this->_id,$index);
        }
    }
    
    
    // renders a picture of the listing
    // with an address and a rent amount
    class ListingTile {
        
    }
    
    // renders a cell with a picture of the listing
    // and also displays information specified from the database
    
    // these are used to display search results
    class ListingRow extends HTMLControl {
        private $_extra;
        var $listing = NULL;
        
        function __construct($id, array $displayadditional = null) {
            if ($displayadditional) $this->_extra = $displayadditional; // this makes sure that the terms searched for appear in the details
            $listingobj = ListingManager::FetchListing($id);
            $this->listing = $listingobj;
            
            // now put it together
            parent::__construct("table",null,array("class"=>"listingrow_holder wide")); // the holder
            
            $toprow = new HTMLControl("tr");
            
            $leftcontainer = new HTMLControl("td",null,array("rowspan" => 2, "class" => "listingrow_left"));
            $image = new HTMLControl("img",null,array("src" => $listingobj->GetThumbnail(), "border" => 0));
            $link = new HTMLControl("a",$image,array("href" => "listing.php?action=view&id=$id"));
            $label = new HTMLControl("div","$" . $listingobj->GetRent(),null,array("font-size"=>"15pt","text-align"=>"center"));

            $status = $this->listing->info['listed'];
            $statuslabel = NULL;
           
            $statuslabel = new HTMLControl("div","Not available for rent",array("class" => "unlisted_label")); 
            if ($status) {
                $statuslabel = new HTMLControl("div","Lease begins on: " . date("m/d/Y",$this->listing->info['availableon']), array("class" => "listed_label"));
            }
            
            /* For searching only */
            /*
                need to find a way to determine the % of the search that this listing matches
                *** NOTE *** can also use searching to create a "find similar listings" feature
            */
            /* * * * * * * * * * */
            
            $leftcontainer->AddChild($link);
            $leftcontainer->AddChild($label);
            $leftcontainer->AddChild($statuslabel);
            $toprow->AddChild($leftcontainer);
            
            $addresscell = new HTMLControl("td");

            $favorited = ($listingobj->IsFavorite() ? "favorite" : "favorite_bw");
            $favorites_button = new HTMLControl(
                "img",
                "",
                array(
                    "src" => ThemeManager::GetImage($favorited),
                    "id" => "favoriteimage_" . $listingobj->GetID(),
                    "onClick" => "favorite(" . $listingobj->GetID() . ")"
                ),
                array(
                    "padding-left" => "8px",
                    "padding-right" => "8px",
                    "cursor"    => "pointer"
                )
            );

            $address = $listingobj->GetAddress(false, true);
            $address->AddChild($favorites_button);
            $address->attributes->Set("class","listingrow_address");
            $addresscell->AddChild($address);
            $toprow->AddChild($addresscell);
            
            $nextrow = new HTMLControl("tr");
            $detailcell = new HTMLControl("td",null,array("valign"=>"top"));
            
            $links = new LinkList();
            $links->SetStyle("border-bottom","solid 1px gray");
            $links->AddLink("listing.php?action=view&id=$id","View Listing");
            
            if (!AuthManager::CheckPermissions(AuthConstants::LANDLORDS))
                $links->AddLink("profile.php?id=" . $listingobj->GetLandlord(true),"Landlord Contact Info");
            
            if (AuthManager::CheckPermissions(AuthConstants::ADMINISTRATORS) ||
               (AuthManager::CheckPermissions(AuthConstants::LANDLORDS) && AuthManager::ListingBelongsTo($id))) {
                $links->AddLink("listing.php?action=edit&id=$id","Edit Listing");
                $links->AddLink("photos.php?id=$id","Manage Photos");
            }
            /*
            else if (!AuthManager::CheckPermissions(AuthConstants::LANDLORDS)) {
                $links->AddLink("messages.php?action=compose&listingid=$id","Contact Landlord");
            }
            */
            if (AuthManager::CheckPermissions(AuthConstants::ADMINISTRATORS)) {
                $links->AddLink("unlist.php?id=$id","Remove Listing");
            }
            
            $detailcell->AddChild($links);
            
            $contactbutton = new LinkButton("","Contact Poster");
            
            $detailtable = new HTMLControl("table", null, array("class"=>"blanktable detailtable"));
            foreach ($listingobj->GetImportantDetails() as $title => $data) {
                $thisrow = new HTMLControl("tr");
                $labelcell = new HTMLControl("td",$title . ": ",array("class"=>"label"));
                $datacell = new HTMLControl("td",$data);
                $thisrow->AddChild($labelcell);
                $thisrow->AddChild($datacell);
                
                $detailtable->AddChild($thisrow);
            }
            $detailcell->AddChild($detailtable);
            $nextrow->AddChild($detailcell);
            
            $this->AddChild($toprow);
            $this->AddChild($nextrow);
        }
    }
?>
